package com.hsl.housaileibot001.controller;

import com.hsl.housaileibot001.common.Result;
import com.hsl.housaileibot001.model.dto.RefreshTokenDTO;
import com.hsl.housaileibot001.model.dto.UserLoginDTO;
import com.hsl.housaileibot001.model.dto.UserRegisterDTO;
import com.hsl.housaileibot001.model.vo.LoginVO;
import com.hsl.housaileibot001.model.vo.UserInfoVO;
import com.hsl.housaileibot001.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制器
 *
 * @author liul
 * @date 2025/01/27
 */
@Tag(name = "认证接口", description = "用户认证相关接口")
@RestController
@RequestMapping("/auth")
public class AuthController {

    @Resource
    private UserService userService;

    @Operation(summary = "用户注册", description = "用户注册，返回access token和refresh token")
    @PostMapping("/register")
    public Result<LoginVO> register(@Valid @RequestBody UserRegisterDTO registerDTO) {
        try {
            LoginVO loginVO = userService.register(registerDTO);
            return Result.success(loginVO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "用户登录", description = "用户名密码登录，返回access token和refresh token")
    @PostMapping("/login")
    public Result<LoginVO> login(@Valid @RequestBody UserLoginDTO loginDTO) {
        try {
            LoginVO loginVO = userService.login(loginDTO);
            return Result.success(loginVO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "刷新Token", description = "使用refresh token刷新access token")
    @PostMapping("/refresh")
    public Result<String> refreshToken(@Valid @RequestBody RefreshTokenDTO refreshTokenDTO) {
        try {
            String accessToken = userService.refreshToken(refreshTokenDTO);
            return Result.success(accessToken);
        } catch (Exception e) {
            return Result.error(401, e.getMessage());
        }
    }

    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户的信息")
    @GetMapping("/me")
    public Result<UserInfoVO> getCurrentUser() {
        try {
            UserInfoVO userInfo = userService.getCurrentUser();
            return Result.success(userInfo);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "退出登录", description = "退出登录，删除refresh token")
    @PostMapping("/logout")
    public Result<Void> logout(
            @Parameter(description = "Refresh Token") @RequestHeader(value = "Refresh-Token", required = false) String refreshToken) {
        try {
            userService.logout(refreshToken);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}

